/**
 * @format
 * @Author: TC
 * @Date: 2024-08-22 15:17:14
 * @Last Modified by: TC
 * @Last Modified time: 2024-08-22 15:24:32
 */

import {
    Camera,
    color,
    director,
    gfx,
    Layers,
    Material,
    Node,
    renderer,
    RenderTexture,
    Sprite,
    SpriteFrame,
    Texture2D,
    UITransform,
    v3,
    view,
} from "cc";
import { nsBundleConst } from "../../../Script/Const/BundleConst";
import { app } from "../../App";
import { ASSERT_TYPE } from "../Res/ResConst";
import { IPEInfoInstance } from "./PostEffectInterface";

/**
 * 后处理基类
 */
export abstract class PostEffectBase {
    protected _info: IPEInfoInstance = null;
    private _currRt: RenderTexture = null; //本pass的rt图
    private _peCameraNode: Node = null; //后处理相机节点
    private _peCamera: Camera = null; //后处理相机
    private _mainCameraNode: Node = null; //主相机
    private _peSprtieNode: Node = null; //渲染节点
    private _rt: RenderTexture = null; //rt图
    private _preRt: RenderTexture = null; //上一个pass的rt图

    private _node: Node = null;

    constructor() {}

    /**
     * 初始化
     */
    protected _init(): void {
        let winSize = view.getVisibleSize();
        this._node = new Node(this._info.name);
        app.sceneMgr.getCurrCanvasNode().addChild(this._node);
        //添加渲染节点
        this._peSprtieNode = new Node("nodePostEffect");
        this._peSprtieNode.layer = 1 << app.postEffectMgr.index;
        let sp: Sprite = this._peSprtieNode.addComponent(Sprite);
        sp.spriteFrame = null;
        this._node.addChild(this._peSprtieNode);
        let transform = this._peSprtieNode.getComponent(UITransform);
        transform.width = winSize.width;
        transform.height = winSize.height;
        //rt图反向采样
        if (this._info.mtl) {
            this._peSprtieNode.setScale(v3(1, -1, 1));
        }
        //添加摄像机节点
        this._peCameraNode = new Node("peCarema");
        this._peCameraNode.setPosition(v3(0, 0, 1000));
        this._peCameraNode.layer = 1 << app.postEffectMgr.index;
        this._node.addChild(this._peCameraNode);
        let camera = (this._peCamera = this._peCameraNode.addComponent(Camera));
        camera.visibility = 1 << app.postEffectMgr.index;
        camera.clearFlags = gfx.ClearFlagBit.COLOR;
        camera.clearColor = color(244, 0, 0, 0);
        camera.projection = renderer.scene.CameraProjection.ORTHO;
        camera.orthoHeight = winSize.height / 2;
        //创建rt图
        this._rt = new RenderTexture();
        this._rt.reset({
            width: winSize.width,
            height: winSize.height,
        });
        this._peCamera.targetTexture = this._rt;
        //层级自加
        app.postEffectMgr.index++;
    }

    /**
     * 安装
     */
    public loadPass(): void {
        this._init();
        this._preRt = app.postEffectMgr.rt;
        let sp: Sprite = this._peSprtieNode.getComponent(Sprite);

        let sf: SpriteFrame = new SpriteFrame();
        sf.texture = this._preRt;
        sp.spriteFrame = sf;
        if (this._info.mtl) {
            app.shaderMgr.changeMaterial(sp, this._info.mtl, () => {
                app.shaderMgr.setTextureProperty(sp.getMaterialInstance(0), "mainTexture", this._preRt);
            });
        } else {
            // sp.spriteFrame = sf;
        }
        this.nextPass();
    }

    /**
     * 卸载
     */
    public unloadPass(): void {
        this._node.destroy();
    }

    public nextPass(): void {
        app.postEffectMgr.rt = this._rt;
    }

    public getRt(): RenderTexture {
        return this._rt;
    }

    /**
     * 刷新回调
     */
    public onUpdate(): void {}
}
